﻿using Hsp.Auto.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hsp.Auto.Generator.Utility
{
    /// <summary>
    /// 主表单扩展类
    /// </summary>
    public static class MainExt
    {
        #region 表实体名称

        /// <summary>
        /// 处理Asp.Net Core EF6 实体转换后的表实体名称(单数)
        /// 规则：去掉“_”分隔符，并每个分隔区域的首字符大写，其余不变
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="isPluralize">是否复数形式：0-否；1-是；</param>
        /// <returns></returns>
        public static string ToSingularModelName(this string tableName)
        {
            return ToModelName(tableName, false);
        }

        /// <summary>
        /// 处理Asp.Net Core EF6 实体转换后的表实体名称(复数)
        /// 规则：去掉“_”分隔符，并每个分隔区域的首字符大写，其余不变
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="isPluralize">是否复数形式：0-否；1-是；</param>
        /// <returns></returns>
        public static string ToPluralizeModelName(this string tableName, bool isPluralize = false)
        {
            return ToModelName(tableName, true);
        }

        /// <summary>
        /// 处理Asp.Net Core EF6 实体转换后的表实体名称
        /// 规则：去掉“_”分隔符，并每个分隔区域的首字符大写，其余不变
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="isPluralize">是否复数形式：0-否；1-是；</param>
        /// <returns></returns>
        public static string ToModelName(this string tableName, bool? isPluralize = null)
        {
            if (tableName == null) return "Null";
            var name = tableName.Trim();
            if (name.IndexOf("_") > -1)
            {
                name = string.Empty;
                var arr = tableName.Split('_');
                foreach (string s in arr)
                {
                    name += s.ToUpperFirst();
                }
            }
            else
            {
                name = name.ToUpperFirst();
            }

            return isPluralize == null ? name : (bool)isPluralize ? new Pluralizer().Pluralize(name) : new Pluralizer().Singularize(name);
        }

        #endregion

        /// <summary>
        /// 字符串改为首字符大写，其余小写
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string ToUpperFirst(this string str)
        {
            if (string.IsNullOrEmpty(str)) return "";

            if (str.ToLower() == "qq") str = "Qq";
            if (str.ToLower() == "id") str = "Id";
            if (str.ToLower() == "pid") str = "Pid";
            if (str.Contains("QQ")) str = str.Replace("QQ", "Qq");
            if (str.Contains("ID")) str = str.Replace("ID", "Id");
            if (str.Contains("API")) str = str.Replace("API", "Api");

            //str = str.ToLower();
            char[] a = str.ToCharArray();
            a[0] = char.ToUpper(a[0]);
            return new string(a);
        }

        /// <summary>
        /// 字符串改为首字符小写
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string ToLowerFirst(this string str)
        {
            if (string.IsNullOrEmpty(str)) return "";
            char[] a = str.ToCharArray();
            a[0] = char.ToLower(a[0]);
            return new string(a);
        }








    }
}
